require(['loading'], function(loading) {
	var params = getUrlParams(); // 获取上页面参数键值
	var category1 = params.category1?params.category1:"";
	var type = params.type;
	var _secondAddressId = "";
	if(type == 3){
		var indexCity = sessionStorage.getItem('indexCity');
		_secondAddressId = sessionStorage.getItem('indexCityId');
		if(indexCity != null){
			$('#cityName2').text(indexCity);
		}else{
			$('#cityName2').text(city);
		}
	}
	// 激活所选择的栏目
	$(".tab li").removeClass("active");
	$(".tab > li").eq(type - 1).addClass("active");
	if (category1 != "") {
		switch(category1) {
			case "1" :
				$("#className").html("水果");
				break;
			case "2" :
				$("#className").html("蔬菜");
				break;
			case "3" :
				$("#className").html("粗粮");
				break;
			case "5" :
				$("#className").html("畜牧");
				break;
			case "6" :
				$("#className").html("水产");
				break;
			case "0" :
				$("#className").html("全部");
				category1="";
				break;
			default:
				break;
		}
	}

	var pageNo = 1;
	var level = 1;
	var topClass2 = '分类';
	//应用实例化
	var bsList = new Vue({
		el: '#bsList',
		data: {
			goodsList: [],
			provinceData:[],
			classData: [],
			showLoading: false,
			isProv: false,
			noData: false,
			apiUrl: wxUrl + '/v3.2/large/list',
			areaUrl: wxUrl + '/v3.2/addr/areaList',
			classUrl: wxUrl + '/v3.2/goodsClass/findSonGoodsClass',
			item: {
				params:{
					token: token,
					userToken: userToken,
					lng: longitude,
					lat: latitude,
					pageNo : pageNo,
					pageSize : 20,
					type: type,
					firstAddressId: '',
					secondAddressId: _secondAddressId,
					category1: category1,
					category2: '',
					category3: ''
				}
			},
			areaItem: {
				params:{
					token: token,
					parentAreaId: ''
				}
			},
			classItem: {
				params:{
					token: token,
					parentId: ''
				}
			}
		},
		methods: {
			moreFn: function() {
				this.$http.get(this.apiUrl, this.item)
				.then(function(response) {
					var result = JSON.parse(response.body);
					if (result.code == 0) {
						var goodsData = result.data.list;
						this.goodsList = this.goodsList.concat(goodsData);
						if(goodsData.length == 0 && this.item.params.pageNo == 1){
							this.noData = true;
						}else{
							this.noData = false;
						}						
					}
				});
			},
			getMore: function() {
				var contiueFlag = this.goodsList.length < this.item.params.pageNo * 20;
				if (contiueFlag) { // 如果没有更多数据，则不调用远程
					promptBox('没有更多数据了！');
					return;
				}
				this.item.params.pageNo++;
				this.moreFn();
			},
			init: function() {
				this.moreFn();
			},
			getProvince: function(){ //地区筛选
				this.areaItem.params.parentAreaId = '';
				this.isProv = true;
				this.$http.get(this.areaUrl, this.areaItem)
				.then(function(response) {
					var result = JSON.parse(response.body);
					this.$set('provinceData', result.data.result);
					//模拟ios下拉
					var showProvinceDom = document.querySelector('#selectProvince');
				    var provinceIdDom = document.querySelector('#provinceId');
				    var provinceId = showProvinceDom.dataset['id'];
			        var provinceName = showProvinceDom.dataset['value'];
			        var provinceSelect = new IosSelect(
			        	1,
			            [result.data.result],
			            {
			                container: '.selectProvince',
			                cssUnit:'rem',
			                headerHeight: 0.5,
			                itemHeight: 0.48,
			                itemShowCount: 5,
			                oneLevelId: provinceId,
			                callback: function (selectOneObj) {
			                    provinceIdDom.value = selectOneObj.id;
			                    $('#cityName2').text(selectOneObj.name);
			                    showProvinceDom.dataset['id'] = selectOneObj.id;
			                    showProvinceDom.dataset['value'] = selectOneObj.name;
			                }
			        	}
			        );
					
				});
			},
			getClass: function(){ //种类筛选
				var oneClassData = [
				    {'id': '1', 'name': '水果', 'parentId': '0'},
				    {'id': '2', 'name': '蔬菜', 'parentId': '0'},
				    {'id': '3', 'name': '畜牧', 'parentId': '0'},
				    {'id': '4', 'name': '药材', 'parentId': '0'}
				];
				var twoClassData = [
				    {"id": "11", "name": "苹果", "parentId": "1"},
				    {"id": "12", "name": "樱桃", "parentId": "1"},
				    {"id": "13", "name": "柿子", "parentId": "1"},
				    {"id": "21", "name": "白菜", "parentId": "2"},
				    {"id": "22", "name": "菠菜", "parentId": "2"},
				    {"id": "23", "name": "山药", "parentId": "2"},
				    {"id": "31", "name": "羊", "parentId": "3"},
				    {"id": "32", "name": "牛", "parentId": "3"},
				    {"id": "33", "name": "鸡", "parentId": "3"}
				];
				
			    var showClassDom = $('#className');
			    var oneClassId = $('#oneClassId');
			    var twoClassId = $('#twoClassId');
		        var oneLevelId = showClassDom.attr('data-oneClass-id');
		        var twoLevelId = showClassDom.attr('data-twoClass-id');
		        var classSelect = new IosSelect(
		        	2,
		            [oneClassData, twoClassData],
		            {
		            	container: '.selectClass',
		                cssUnit:'rem',
		                headerHeight: 0.5,
		                itemHeight: 0.48,
		                itemShowCount: 5,
		                relation: [1, 0, 0, 0],
		                oneLevelId: oneLevelId,
		                twoLevelId: twoLevelId,
		                callback: function (selectOneObj, selectTwoObj) {
		                	if(selectTwoObj.atindex == 3){
		                		oneClassId.val(selectOneObj.id); 
		                    	oneClassId.attr('data-oneClass-name', selectOneObj.name);
		                    	showClassDom.attr('data-oneClass-id', selectOneObj.id);
		                    	showClassDom.html(selectOneObj.name);
		                	}else{
		                		oneClassId.val(selectOneObj.id); 
			                    oneClassId.attr('data-oneClass-name', selectOneObj.name);
			                    twoClassId.val(selectTwoObj.id);
			                    twoClassId.attr('data-twoClass-name', selectTwoObj.name);
			                    showClassDom.attr('data-oneClass-id', selectOneObj.id);
			                    showClassDom.attr('data-twoClass-id', selectTwoObj.id);
			                    showClassDom.html(selectTwoObj.name);
		                	}
		                }
		        	}
		        );
			},
			getSort: function(){ //排序筛选
				this.areaItem.params.parentAreaId = ''; //清除排序的参数
				var sortData = [
					{'id': '1', 'name': '默认排序'},
					{'id': '2', 'name': '按时间倒叙'},
					{'id': '3', 'name': '按时间正序'},
					{'id': '4', 'name': '按价格最低'},
					{'id': '5', 'name': '按价格最高'}
				];
				//模拟ios下拉 排序
				var showSortDom = document.querySelector('#selectSort');
			    var sortIdDom = document.querySelector('#sortId');
			    var sortId = showSortDom.dataset['id'];
		        var sortName = showSortDom.dataset['value'];
		        var sortSelect = new IosSelect(
		        	1,
		            [sortData],
		            {
		                container: '.selectSort',
		                cssUnit:'rem',
		                headerHeight: 0.5,
		                itemHeight: 0.48,
		                itemShowCount: 5,
		                oneLevelId: sortId,
		                callback: function (selectOneObj) {
		                    sortIdDom.value = selectOneObj.id;
		                    showSortDom.dataset['id'] = selectOneObj.id;
		                    showSortDom.dataset['value'] = selectOneObj.name;
		                }
		        	}
		        );
			},
			goSearch: function(){
				window.location.href = '../bigSupply/search.html';
			}
		},
		directives: { // 自定义v-scroll指令
			scroll: {
				bind: function(el, binding) {
					window.addEventListener('scroll', function() {
						if($(window).scrollTop() + $(window).height() == $(document).height()) {
							bsList.getMore();
						}
					})
				}
			}
		}
	});
	bsList.init(); //pageNo=1
	
	//选择城市 & 分类
	$(function(){
		//打开分类选项
		$("#selectClass").click(function(){
			$("#goodsClass").animate({"bottom": 0});
		});
		//关闭分类选项
		$(".class_header").click(function(){
			$("#goodsClass").animate({"bottom": "-100%"});
		});
		// tab切换
		$(".tab li").click(function(){
			var beforeType = $(".tab li.active").attr("type");
			var type = $(this).attr("type");
			if (beforeType == type) {
				if(type == 1 || type == 2){
					return ;
				}
			}
			$(".tab li").removeClass("active");
			$(this).addClass("active");
			if(type == 1 || type == 2){
				bsList.goodsList = [];
			}
			bsList.item.params.type= type;
			type = type;
			bsList.item.params.pageNo= 1;
			var areaClass = $('#selectCity').hasClass('active');
			var className = $('#selectClass').hasClass('active');
			if(!areaClass){
				$('#cityName2').text('地区');
				bsList.item.params.secondAddressId = '';
			}
			if(!className){
				$('#className').text('分类');
				bsList.item.params.category1 = "";
				bsList.item.params.category2 = "";
				bsList.item.params.category3 = "";
			}
			if (type == 3 || type == 4) {
				return ;
			}
			bsList.init();
		});
	});
});
// 为可控制返回指定页面所有添加pushState
history.pushState({}, document.title, null);
pushVHistory();
window.addEventListener("popstate", function() {
    // 跳转到订单列表页面
	goBackVHistory();
});